import React, { Component } from 'react'
import './index.scss'
import axios from '../../components/axios'
import { NavLink } from 'react-router-dom'
import pubsub from 'pubsub-js'
export default class index extends Component {
    constructor(props){
        super(props)
        this.state={
            search:'',
            data:[],
        }
        pubsub.subscribe("search",(msg,data)=>{//搜索时从头部组件接受
            axios("search",{data:data}
            ).then((result)=>{
                this.setState({data:result.data})
            }).catch(err=>{
                console.log(err);
            })
        })
    }
    componentDidMount(){//从标签进入时
        pubsub.publish("component","搜索")
        axios("search",{data:this.props.location.state.data}
            ).then((result)=>{
                this.setState({data:result.data})
                pubsub.publish("page",this.props.location.state.data)
            }).catch(err=>{
                console.log(err);
            })
    }
    render() {
        return (
            <div className="search-inner">
                <nav>
                    <div className="search-header">
                    <h2>{this.props.location.state.data}</h2><p>{this.props.location.state.type}<span>{this.state.data.length}</span></p>
                    </div>
                    {this.state.data.map((item,index)=>{
                        return (
                            <div className="result">
                                <NavLink to={`/content/${item.id}`}>
                                    <p className="time">{(()=>{
                                        return new Date(item.changeTime).toLocaleString('chinese',{ hour12: false });
                                    })()}</p>
                                    <p className="title">{item.title}</p>
                                </NavLink>
                            </div>
                        )
                    })}
                </nav>
            </div>
        )
    }
}
